<?php
namespace BoxAdmin\Controller;

use Think\Controller;

class ContentController extends CommonController
{

    public function _initialize()
    {
        parent::_initialize();
        $this->content = D('SystemContent');
        $this->content_type = BoxModel('system_content_type');
        $this->id = I('id', '', 'intval');
    }

    /**
     * [index 资讯首页]
     * @return [type] [description]
     */
    public function index()
    {
        $this->memuNav = "index";
        $where = "store_id='$this->store_id'";
        $this->catedata = $this->content_type->where("store_id='$this->store_id'")->select();
        $kw = I('get.kw', '', 'trim');
        if ($kw) {
            $where .= ' and title like "%' . $kw . '%" or tip like "%' . $kw . '%" or source like "%' . $kw . '%" or content like "%' . $kw . '%" or author like "%' . $kw . '%" or A.name like "%' . $kw . '%"';
        }
        if ($this->cate_id = I('cate_id')) {
            $where .= " and type_id='$this->cate_id'";
        }
        if ($this->begintime = I('begintime')) {
            $b = strtotime($this->begintime);
            $this->endtime = I('endtime', date("Y-m-s"));
            $s = strtotime($this->begintime);
            $e = strtotime($this->endtime);
            $where .= " and (time>'$b' and time<'$e')";
        }
        $this->model("SystemContent")->where($where)->getList();
        $this->display();
    }

    /**
     * 分类管理
     */
    public function type()
    {
        $this->memuNav = "type";
        $data = $this->content_type->where(array('store_id' => $this->store_id))->select();
        $this->data = getTree($data);
        $this->display();
    }

    public function addType()
    {
        $this->memuNav = "type";
        $data = $this->content_type->where(array('store_id' => $this->store_id))->select();
        $this->list = getTree($data);
        if ($this->id) {
            $this->data = $this->content_type->where(array('id' => $this->id, 'store_id' => $this->store_id))->find();
        }
        $this->display();
    }

    public function insertType()
    {
        if (IS_POST) {
            $data = array(
                'pid' => I('pid'),
                'name' => I('name'),
                'title' => I('title'),
                'time' => time(),
                'store_id' => $this->store_id,
            );
            if (!$this->id) {
                $this->content_type->add($data);
                D('Log')->addLogs(session('user_name'), '添加资讯分类');
                $this->redirect('type');
            } else {
                unset($data['time']);
                unset($data['pid']);
                $this->content_type->where(array('id' => $this->id, 'store_id' => $this->store_id))->save($data);
                D('Log')->addLogs(session('user_name'), '修改资讯分类');
                $this->redirect('type');
            }
        }
    }

    /**
     * 删除分类
     * @return [type]
     */
    public function deleteType()
    {
        $count = $this->content_type->where(array('pid' => $this->id, 'store_id' => $this->store_id))->count();
        if ($count > 0) {
            $this->error('该分类下还有子分类，不能删除！', U('type'));
        } else {
            $this->content_type->where(array('id' => $this->id, 'store_id' => $this->store_id))->delete();
            $this->content->where(array('type_id' => $this->id, 'store_id' => $this->store_id))->delete();
            D('Log')->addLogs(session('user_name'), '删除分类');
            $this->redirect('type');
        }
    }

    /**
     * addContent 添加资讯
     */
    public function addContent()
    {
        $this->memuNav = "index";
        $type = $this->content_type->where(array('store_id' => $this->store_id))->select();
        $this->list = getTree($type);
        if ($this->id) {
            $this->data = $this->content->where(array('id' => $this->id, 'store_id' => $this->store_id))->find();
        }
        $this->display();
    }

    public function contInsert()
    {
        if (IS_POST) {
            if (empty($this->id)) {
                $info = $this->_upload("/content/");
                $data = array(
                    'title' => I('title'),
                    'author' => I('author'),
                    'source' => I('source'),
                    'content' => stripslashes(htmlspecialchars_decode($_POST['content'])), //防止标签转义
                    'time' => time(),
                    'type_id' => I('type_id'),
                    'tip' => I('tip'),
                    'img' => $info['img']['savepath'] . $info['img']['savename'],
                    'store_id' => $this->store_id,
                );
                $this->content->add($data);
                D('Log')->addLogs(session('user_name'), '添加资讯');
                $this->redirect('index');
            } else {
                $info = $this->_upload("/content/");
                if ($info == false) {
                    $arr['img'] = I('oldimg');
                } else {
                    $arr['img'] = $info['img']['savepath'] . $info['img']['savename'];
                }
                $arr['title'] = I('title');
                $arr['author'] = I('author');
                $arr['source'] = I('source');
                $arr['content'] = stripslashes(htmlspecialchars_decode($_POST['content']));//防止标签转义
                $arr['type_id'] = I('type_id');
                $arr['tip'] = I('tip');
                $this->content->where(array('id' => $this->id, 'store_id' => $this->store_id))->save($arr);
                D('Log')->addLogs(session('user_name'), '修改资讯');
                $this->redirect('index');
            }
        }
    }

    /**
     * [delete 删除资讯]
     * @return [type]
     */
    public function delete()
    {
        $this->content->where(array('id' => $this->id, 'store_id' => $this->store_id))->delete();
        $this->redirect('index');
    }

    /**
     * [delall 批量删除]
     * @return [type]
     */
    public function delall()
    {
        $ids = I('ids');
        $id = explode(',', $ids);
        for ($i = 0; $i < count($id); $i++) {
            $this->content->where(array('store_id' => $this->store_id, 'id' => $id[$i]))->delete();
        }
        $this->redirect('index');
    }

    /**
     * 关于我们
     */
    public function about()
    {
        $this->memuNav = "about";
        $where = 'store_id=' . $this->store_id;
        $model = BoxModel('about');
        $this->data = $model->where($where)->find();
        $this->display();
    }

    public function about_add_do()
    {
        if (IS_POST) {

            $data = array(
                'content' => stripslashes(htmlspecialchars_decode($_POST['content'])),
                'info' => stripslashes(htmlspecialchars_decode($_POST['info'])),
                'address' => I('address'),
            );
            $model = BoxModel('about');
            $find = $model->where(array('store_id' => $this->store_id))->find();
            if ($find) {
                $model->where(array('store_id' => $this->store_id))->save($data);
            } else {
                $data['store_id'] = $this->store_id;
                $data['posttime'] = time();
                $model->add($data);
            }
            $this->redirect('Content/about');
        }
    }
}

?>